//
//  Dictionary+PPKit.swift
//  PPKit
//
//  Created by xiaopin on 2022/4/15.
//  Copyright © 2022 sowell.com. All rights reserved.
//

import Foundation

/// 线程安全的字典
class ThreadSafeDict {
    var dict = [String:Any]()
    private let syncQueue = DispatchQueue(label: "ThreadSafeDict Queue",
                                          qos: .default,
                                          attributes: .concurrent,
                                          autoreleaseFrequency: .inherit,
                                          target: nil)
    public init() {}
    
    public func get(key:String) -> Any?{
        return syncQueue.sync {
            dict[key]
        }
    }
    
    public func set(key:String,value:Any){
        syncQueue.sync(flags: .barrier) {
            dict[key] = value
        }
    }
}
